package com.me.modules.chg.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.FieldDictionaryEntity;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.entity.ValueDictionaryEntity;
import com.me.modules.sys.service.FieldDictionaryService;
import com.me.modules.sys.service.OperationService;
import com.me.modules.sys.service.ValueDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.me.modules.chg.entity.ChangeInfoEntity;
import com.me.modules.chg.service.ChangeInfoService;

import javax.servlet.http.HttpServletRequest;


/**
 * 变更工单信息
 *
 * @author lill
 * @email lill5270@163.com
 * @date 2019-01-14 13:54:06
 */
@Controller
@RequestMapping("/chg/changeinfo")
public class ChangeInfoController extends BaseController{
	@Autowired
	private ChangeInfoService changeInfoService;
	@Autowired
	FieldDictionaryService feldDictionaryService;
	@Autowired
	ValueDictionaryService valueDictionaryService;
	@Autowired
	private OperationService operationService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("changeinfo:list")
	@SysLog("查询变更列表")
	public String list(ChangeInfoEntity changeInfo, Model model , HttpServletRequest request){

		Long time =new Date().getTime();
		System.out.println(time);
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<ChangeInfoEntity> page = changeInfoService.findPage(changeInfo, pageNum);
		List<ChangeInfoEntity>  changeList = page.getResult();
		List<ChangeInfoEntity>  newchangeList = new ArrayList<ChangeInfoEntity>();
		List list =new ArrayList();
		List<FieldDictionaryEntity> fieldList=feldDictionaryService.queryByTableName("change");
		if(fieldList.size()>0){

			if( changeList.size()>0){
				for(int i=0;i<changeList.size();i++){
					ChangeInfoEntity change =changeList.get(i);
					String tableId=changeList.get(i).getId().toString();

					List<ValueDictionaryEntity> valueList=new ArrayList<ValueDictionaryEntity>();
					for(int j=0;j<fieldList.size();j++){
						Long fieldID=fieldList.get(j).getId();

						ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
						if(value!=null){
							valueList.add(value);
						}else{
							valueList.add(new ValueDictionaryEntity());
						}
					}
					change.setValueDictionarys(valueList);
					newchangeList.add(change);
				}
			}
			model.addAttribute("page",new Page(newchangeList,page.getTotal(),page.getPageSize() , page.getPageNum()));

		}else{
			model.addAttribute("page",page);

		}
		//model.addAttribute("page",new Page(newleaveList,page.getTotal(),page.getPageSize() , page.getPageNum()));
		model.addAttribute("changeInfo",changeInfo);
		model.addAttribute("fieldList",fieldList);
		Long newtime =new Date().getTime();
		System.out.println(newtime-time);
		return "change/change";
	}


	/**
	 * 信息
	 */
	@RequestMapping("info")
	@RequiresPermissions("changeinfo:info")
	@SysLog("查询变更工单信息")
	public String info(Model model, Long id, HttpServletRequest request){
		List<FieldDictionaryEntity> fieldList=feldDictionaryService.queryByTableName("change");
		if(id!=null){
			ChangeInfoEntity changeInfo = changeInfoService.queryObject(id);
			if(fieldList.size()>0){
				String tableId=changeInfo.getId().toString();
				List<ValueDictionaryEntity> valueList=new ArrayList<ValueDictionaryEntity>();
				for(int j=0;j<fieldList.size();j++){
					Long fieldID=fieldList.get(j).getId();

					ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
					if(value!=null){
						valueList.add(value);
					}else{
						valueList.add(new ValueDictionaryEntity());
					}
				}
				changeInfo.setValueDictionarys(valueList);
			}
			model.addAttribute("changeInfo", changeInfo);
		}else{
			ChangeInfoEntity changeInfo = new ChangeInfoEntity();
			changeInfo.setCode(Utils.getSN("CHG_","yyyyMMddHHmmss",2));
			model.addAttribute("changeInfo", changeInfo);
		}

		model.addAttribute("fieldList",fieldList);
		List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
		model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
		return "change/changeEdit";
		//IncidentInfoEntity incidentInfo = incidentInfoService.queryObject(id);

		//return Result.ok().put("incidentInfo", incidentInfo);
	}

	/**
	 * 编辑
	 * @param changeInfo
	 * @return
	 */
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@RequiresPermissions("changeinfo:save")
	@ResponseBody
	public Result edit(ChangeInfoEntity changeInfo){
		if(changeInfo.getId()==null){
			changeInfoService.save(changeInfo);
			if(changeInfo.getValueDictionarys().size()>0) {
				for (ValueDictionaryEntity value : changeInfo.getValueDictionarys()) {
					if(!StringUtils.isEmpty(value.getFieldValue())){
						value.setTableId(changeInfo.getId().toString());
						valueDictionaryService.save(value);
					}
				}
			}
		}else {
			changeInfoService.update(changeInfo);
			if(changeInfo.getValueDictionarys().size()>0) {
				for (ValueDictionaryEntity value : changeInfo.getValueDictionarys()) {
					ValueDictionaryEntity valueDictionaryEntity=valueDictionaryService.findByFieldIdAndUserId(value.getFieldId(),changeInfo.getId().toString());
					if(valueDictionaryEntity!=null){
						valueDictionaryService.delete(valueDictionaryEntity.getId());
					}
					if(!StringUtils.isEmpty(value.getFieldValue())){
						value.setTableId(changeInfo.getId().toString());
						valueDictionaryService.save(value);
					}


				}
			}
		}
		return Result.ok();
	}


	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("changeinfo:save")
	public Result save(@RequestBody ChangeInfoEntity changeInfo){
		changeInfoService.save(changeInfo);

		return Result.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("changeinfo:update")
	public Result update(@RequestBody ChangeInfoEntity changeInfo){
		changeInfoService.update(changeInfo);

		return Result.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("changeinfo:delete")
	public Result delete(@RequestBody Long[] ids){
		changeInfoService.deleteBatch(ids);

		return Result.ok();
	}

}
